#import "WIFIViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <SystemConfiguration/CaptiveNetwork.h>

@interface WIFIViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) NSArray *currentNetworkInfos;
@property (weak, nonatomic) IBOutlet UILabel *ssidLabel;
@property (weak, nonatomic) IBOutlet UILabel *bssidLabel;

@end

@implementation WIFIViewController

- (NSArray *)currentNetworkInfos
{
    if (!_currentNetworkInfos) {
        
        _currentNetworkInfos = [NSArray arrayWithArray:[SSID fetchNetwrokInfo]];
    }
    return _currentNetworkInfos;
}

- (CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc]init];
    }
    return _locationManager;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (@available(iOS 13.0, *)) {
        CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
        if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
            [self updateWIFI];
        }else {
            self.locationManager.delegate = self;
            [self.locationManager requestWhenInUseAuthorization];
        }
    }else {
        [self updateWIFI];
    }
}

- (void)updateWIFI
{
    NetworkInfo *info = [self.currentNetworkInfos firstObject];
    self.ssidLabel.text = info.ssid;
    self.bssidLabel.text = info.bssid;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self updateWIFI];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end


@implementation SSID

+ (NSArray <NetworkInfo *>*)fetchNetwrokInfo
{
    NSArray *interfaces = CFBridgingRelease(CNCopySupportedInterfaces());
    NSMutableArray *networkInfos = [NSMutableArray array];
    for (NSString *interface in interfaces) {
         NSDictionary *info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)interface);
        NetworkInfo *networkInfo = [[NetworkInfo alloc]initWith:interface success:YES ssid:@"" bssid:@""];
        NSString *ssid = (__bridge NSString *)kCNNetworkInfoKeySSID;
        NSString *bssid = (__bridge NSString *)kCNNetworkInfoKeyBSSID;
        networkInfo.ssid = info[ssid];
        networkInfo.bssid = info[bssid];
        [networkInfos addObject:networkInfo];
    }
    return networkInfos;
}

@end

@implementation NetworkInfo

- (instancetype)initWith:(NSString *)interface success:(BOOL)suc ssid:(NSString *)ssid bssid:(NSString *)bssid
{
    if (self == [super init]) {
        self.interface = interface;
        self.success = suc;
        self.ssid = ssid;
        self.bssid = bssid;
    }
    return self;
}

- (instancetype)initWithData:(NSDictionary *)params {
    if (self == [super init]) {
        /*dlsdllad*/
    }
    
    return self;
}

@end
